<template>
  <a-modal
    title="编辑EMS订单物料"
    :width="1200"
    :maskClosable="false"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :footer="hasPerm('emsOrderMaterial:editProduct') ? undefined : null"
    @cancel="handleCancel">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row>
          <a-form-item v-show="false">
            <a-input placeholder="请输入订单物料ID"
                     v-decorator="['orderId', {rules: [{required: true, message: '请输入订单物料ID！'}]}]"/>
            <a-input placeholder="请输入订单物料ID"
                     v-decorator="['orderMaterialId', {rules: [{required: true, message: '请输入订单物料ID！'}]}]"/>
            <a-input placeholder="请输入物料ID"
                     v-decorator="['materialId', {rules: [{required: true, message: '请输入物料ID！'}]}]"/>
            <a-input placeholder="请输入BOMID"
                     v-decorator="['bomId', {rules: [{required: true, message: '请输入BOMID！'}]}]"/>
          </a-form-item>
          
          <a-col :span="12">
            <a-form-item
              label="产品"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback=""
            >
              <a-input placeholder="请输入产品名称"
                       v-decorator="['productName', {rules: [{required: true, message: '请输入产品名称！'}]}]"/>
            </a-form-item>
          </a-col>
          
          <a-col :span="12">
            <a-form-item
              label="序号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback=""
            >
              <a-input placeholder="请输入序号"
                       v-decorator="['extend1', {rules: [{required: true, message: '请输入序号！'}]}]"/>
            </a-form-item>
          </a-col>
          
          <a-col :span="12">
            <a-form-item
              label="软件版本号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback=""
            >
              <a-input placeholder="请输入软件版本号"
                       v-decorator="['extend2', {rules: [{required: false, max: 25, message: '请输入不大于25位长度软件版本号！'}]}]"/>
            </a-form-item>
          </a-col>
          
          <a-col :span="12">
            <a-form-item
              label="数量"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback=""
            >
              <a-input placeholder="请输入数量"
                       v-decorator="['number', {rules: [{required: true, message: '请输入数量！'}]}]"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item
              label="备注"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
              has-feedback=""
            >
              <a-input placeholder="请输入备注"
                       v-decorator="['remark', {rules: [{required: false, max: 99, message: '请输入不大于99位长度备注！'}]}]"/>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
    <template slot="footer">
      <div>
        <a-button type="white" @click="handleCancel">取消</a-button>
        <a-button type="primary" @click="handleSubmit">确定</a-button>
      </div>
    </template>
  </a-modal>
</template>

<script>
import {editProduct} from '@/api/modular/main/emsordermaterial/emsOrderMaterialManage'

export default {
  data() {
    return {
      labelCol: {
        xs: {span: 24},
        sm: {span: 5}
      },
      wrapperCol: {
        xs: {span: 24},
        sm: {span: 15}
      },
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this)
    }
  },
  methods: {
    edit(record) {
      this.visible = true
      setTimeout(() => {
        this.form.setFieldsValue(record)
        this.form.setFieldsValue({
          productName: record.materialName,
          remark: record.material.remark
        })
      }, 100)
    },
    handleSubmit() {
      const {form: {validateFields}} = this
      this.confirmLoading = true
      
      validateFields((errors, values) => {
        if (!errors) {
          editProduct(values).then((res) => {
            this.confirmLoading = false
            if (res.success) {
              this.$message.success('编辑成功')
              this.handleCancel()
              this.$emit('ok', values)
            } else {
              this.$message.info('编辑失败：' + res.message)
            }
          }).finally((res) => {
            this.confirmLoading = false
          })
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.form.resetFields()
      this.visible = false
    }
  }
}
</script>
